package com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyActivity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.diudiutechnology.www.yadiudiu.util.http.HttpClient;
import com.diudiutechnology.www.yadiudiu.util.http.JsonTools;
import com.diudiutechnology.www.yadiudiu.util.utils.CountDownButtonHelper;
import com.diudiutechnology.www.yadiudiu.util.utils.MyProgressDialog;
import com.diudiutechnology.www.yadiudiu.util.utils.SharedPreferencesUtil;
import com.diudiutechnology.www.yadiudiu.util.utils.Sundry;
import com.diudiutechnology.www.yadiudiu.util.utils.ToastUtils;
import com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyApplication;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.RegistrationModel;
import com.diudiutechnology.www.yadiudiu.R;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.xutils.common.Callback;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.jpush.android.api.JPushInterface;

/**
 * Created by Administrator on 2017/8/16.
 * 快捷登录
 */

public class QuickLoginActivity extends BaseActivity implements View.OnClickListener,Callback.CommonCallback<String> {
    @Bind(R.id.retur)
    LinearLayout retur;
    @Bind(R.id.pag_ren_text)
    TextView pagRenText;
    @Bind(R.id.title_right)//注册
    TextView title_right;

    @Bind(R.id.phonenumber)//手机号
            EditText phonenumber;

    @Bind(R.id.admin_yzm)//验证码
            EditText admin_yzm;
    @Bind(R.id.admin_btn_yzm)//获取验证
            Button admin_btn_yzm;

    @Bind(R.id.button)//注册
            Button button;

    @Bind(R.id.wechat_login_layout)//微信登陆
    LinearLayout wechat_login_layout;
   private Context context;
    private String Judge_interface="0";//判断哪个接口   0、短信  1、登录
  public static QuickLoginActivity quickLoginActivity;
    private MyProgressDialog mDialog;
    private IWXAPI api;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quick_login);
        ButterKnife.bind(this);

        initView();
    }

    private void initView() {
        quickLoginActivity=this;
        context=this;
        initData();
    }

    private void initData() {
        pagRenText.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG);
        retur.setOnClickListener(this);
        pagRenText.setOnClickListener(this);
        admin_btn_yzm.setOnClickListener(this);
        button.setOnClickListener(this);
        title_right.setOnClickListener(this);
        wechat_login_layout.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
            switch (view.getId()){
                case R.id.retur:
                    finish();
                    break;
                case R.id.pag_ren_text:
                    finish();
                    break;
                case R.id.wechat_login_layout:
                    if (api == null) {
                        api = WXAPIFactory.createWXAPI(context, MyApplication.WX_APPID, true);
                    }
                    SendAuth.Req req = new SendAuth.Req();
                    req.scope = "snsapi_userinfo";
                    req.state = "none";
                    api.sendReq(req);
                    break;
                case R.id.title_right://注册
                    Intent intent=    new Intent(QuickLoginActivity.this,RegistrationActivity.class);
                    intent.putExtra("pos","1");
                    startActivity(intent);
                    break;
                case R.id.admin_btn_yzm://短信
                    if (TextUtils.isEmpty(phonenumber.getText().toString().trim())){
                        ToastUtils.show(context, getResources().getString(R.string.please_phone_number));
                        return;
                    }
                    Judge_interface="0";
                    setPhonenumber();
                    break;
                case R.id.button://注册
                    if (TextUtils.isEmpty(phonenumber.getText().toString().trim())){//手机号
                        ToastUtils.show(context, getResources().getString(R.string.please_phone_number));
                        return;
                    }
                    if (!Sundry.isMobileNO(phonenumber.getText().toString().trim())){//验证手机号
                        ToastUtils.show(context, getResources().getString(R.string.please_correctness_phone_number));
                        return;
                    }
                    if (TextUtils.isEmpty(admin_yzm.getText().toString().trim())){//验证码
                        ToastUtils.show(context, getResources().getString(R.string.please_verification_code));
                        return;
                    }
                    Judge_interface="1";
                    setPhonenumber();
                    break;
            }
    }
    //提交短信 和登录
    public void setPhonenumber(){
        if (Sundry.isOpenNetwork(context)){
            mDialog =new MyProgressDialog(context);
        if (Judge_interface.equals("0")){
            HttpClient.note(phonenumber.getText().toString().trim(), "3", this);
        }else {
         HttpClient.Text_login(phonenumber.getText().toString().trim(),admin_yzm.getText().toString().trim(), JPushInterface.getRegistrationID(this), this);
        }
        }else {
            ToastUtils.Toastshow(context, getResources().getString(R.string.network));
        }
    }
    @Override
    public void onSuccess(String result) {
        if (mDialog!=null){
            mDialog.dismiss();
        }
        List<RegistrationModel> getnote = JsonTools.getnote(result);
        if (getnote!=null||getnote.size()>0){
            String status = getnote.get(0).getStatus();
            String getMsg = getnote.get(0).getMsg();
            if (status.equals("200")){
                if (Judge_interface.equals("0")){
                    // 验证码
                    CountDownButtonHelper helper = new CountDownButtonHelper(admin_btn_yzm, 120, 1);
                    helper.start();
                    ToastUtils.show(context, getMsg);
                }else {

                    String userid = getnote.get(0).getUserid();
                    String token = getnote.get(0).getToken();
                    System.out.println("stringmd5"+token);
                    SharedPreferencesUtil.putString(SharedPreferencesUtil.Keys.USERID_KEYS,userid);
                    SharedPreferencesUtil.putString(SharedPreferencesUtil.Keys.TOKEN_KEYS,token);
                    SharedPreferencesUtil.putString(SharedPreferencesUtil.Keys.USERID_KEYS,userid);
                    ToastUtils.show(context, getMsg);
                    LoginActivity loginAct=new LoginActivity();
                    loginAct.loginActivity.finish();
                    finish();
                }

            }else {
                ToastUtils.show(context, getMsg);
            }
        }
    }

    @Override
    public void onError(Throwable ex, boolean isOnCallback) {
        if (mDialog!=null){
            mDialog.dismiss();
        }
        ToastUtils.Toastshow(context, getResources().getString(R.string.server));
    }

    @Override
    public void onCancelled(CancelledException cex) {
        if (mDialog!=null){
            mDialog.dismiss();
        }
        ToastUtils.Toastshow(context, getResources().getString(R.string.server));
    }

    @Override
    public void onFinished() {

    }
}
